package bank;

public class _12IntToRoman {
    public static String intToRoman(int num) {
        int values[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String[] reps={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

        /**
         * 如果按照传统的对数字处理，按位取余，此代码将非常复杂
        int count = 0;
        StringBuilder res = null;
        while (true){
            count = num%10;
            ...
        }*/
        //这里采用另外一种思路：适用于各类有对应关系的数字之间的转化
        //这种算法非常巧妙，遇到大数字直接减掉，同时加上对应字符串
        //符合从高位到低位依次对应的原则！！！
        //出乎意料的是，这种算法效率也不是很低！
        StringBuilder res = new StringBuilder();
        for(int i = 0;i < 13;i++){
            while(num >= values[i]){
                num -= values[i];
                res.append(reps[i]);
            }
        }
        return new String(res);
    }
}
